package com.info.zhiduoduo.repository.mapper.invoice;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.invoice.InvoiceInventoryRecordDto;
import com.info.zhiduoduo.repository.entity.invoice.InvoiceInventoryRecord;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author bob
 * @since 2020-05-24
 */
public interface InvoiceInventoryRecordMapper extends BaseMapper<InvoiceInventoryRecord> {

	@Select(" select iir.*,ms.username,ma.realname " +
			" from zdd_invoice_inventory_record iir " +
			" join zdd_maker_studio ms on ms.userid = iir.userid " +
			" join zdd_maker_account ma on ma.userid = iir.userid " +
			" where CONCAT(ms.username,ma.realname) like '%${name}%' " +
			" and DATE(iir.createtime) between #{startDate} and #{endDate} " +
			" and deleted = 0  " +
			" and if( isnull(#{dockingCity}) or #{dockingCity} = '' ,1=1 ,ma.opening_location in (0${dockingCity})) " +
			" order by id desc ")
	List<InvoiceInventoryRecordDto> findList(@Param("name") String name, @Param("startDate") Date startDate,
											 @Param("endDate") Date endDate,@Param("dockingCity") String dockingCity);


	@Select(" select sum(if(state=2,amount,-amount)) from " +
			" zdd_invoice_inventory_record " +
			" where deleted=0 and state!=1 and userid=#{userid} and invoice_type=#{invoiceType} and amount_type=#{amountType}")
	Integer getInvoiceSum(@Param("userid")Integer userid,@Param("invoiceType")Integer invoiceType,@Param("amountType")Integer amountType);
}
